<?php declare(strict_types=1);


namespace App\Api\V2\Controller\Bill\Merchant;

use App\Api\Controllers\BaseController;
use App\Models\Bi\Bill;
use App\Models\Merchant;
use App\Models\MerchantStore;
use App\Models\Store;
use App\Models\StoreAllPayWayList;
use App\Services\Count\MerchantBillCountService;
use App\Services\Count\UserBillCountService;
use App\Services\Count\UserService;
use App\Services\PrintSqlService;
use App\Traits\ResponseTrait;
use App\Util\TimeUtil;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class WxCountController extends BaseController
{
    use ResponseTrait;


    public function staticCount(Request $request): string
    {
        try {
            $user = $this->parseToken();
            $merchant_id = (int)$user->merchant_id;
            list($storeP, $storeIds) = $this->getMerchantWhere($request, $merchant_id);

            $where = [
                "curHdIndex" => $request->get("curHdIndex"),
                "startDate" => $request->get("startDate"),
                "endDate" => $request->get("endDate"),
                "store_pid" => $storeP->store_id,
                "store_id" => $storeIds,
                "pay_type" => $request->get("pay_type"),
                "company" => $request->get("company"),
                "total_amount" => $request->get("total_amount"),
                "receive_model" => $request->get("receive_model"),
                "cashier_id" => $request->get("merchant_id")
            ];
            $service = new MerchantBillCountService();
            $data = $service->staticCount($where);
            return $this->successResponse("ok", ["data" => $data]);
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }

    //小程序 分组数据列表统计
    public function staticGroupCountList(Request $request): string
    {
        try {
            $user = $this->parseToken();
            $merchant_id = (int)$user->merchant_id;
            list($storeP, $storeIds) = $this->getMerchantWhere($request, $merchant_id);
            $where = [
                "curHdIndex" => $request->get("curHdIndex"),
                "startDate" => $request->get("startDate"),
                "endDate" => $request->get("endDate"),
                "store_pid" => $storeP->store_id,
                "store_id" => $storeIds,
                "pay_type" => $request->get("pay_type"),
                "company" => $request->get("company"),
                "total_amount" => $request->get("total_amount"),
                "receive_model" => $request->get("receive_model"),

                "page" => $request->get("page", 1),
                "pagesize" => $request->get("pagesize", 20),
//                "cashier_id" =>$request->get("merchant_id")
            ];
            $group = $request->get("group", "store_id");
            switch ($group) {
                case "store_pid":
                    $groupData = ["store_pid"];
                    break;
                case "store_id":
                    $groupData = ["store_pid", "store_id"];
                    break;
                case "merchant_id":
                    $groupData = ["store_pid", "store_id", "cashier_id"];
                    break;
                case "pay_type":
                    $groupData = ["store_pid", "pay_type"];
                    break;
                case "day":
                    $groupData = ["store_pid", "day"];
                    break;
                default:
                    throw new \Exception("请设置分组条件");
            }
//            PrintSqlService::listen();
            $service = new MerchantBillCountService();
            $data = $service->staticGroupCountList($where, $groupData);
            return $this->successResponse("ok", ["data" => $data]);
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }


    //查看收银明细
    public function staticBillLogList(Request $request): string
    {
        try {
            $user = $this->parseToken();
            $merchant_id = (int)$user->merchant_id;
            list($storeP, $storeIds) = $this->getMerchantWhere($request, $merchant_id);
            //获取代理商
            $userService = new UserService();
            list($idValues, $column) = $userService->handleUserAccountRelation((int)$storeP->user_id);
            list($oem_id, $agent1_id, $agent2_id, $agent3_id, $agent4_id) = $idValues;
            list($profit_column, $fee_column) = $column;
            $where = [
                "oem_id" => $oem_id,
                "agent1_id" => $agent1_id,
                "agent2_id" => $agent2_id,
                "agent3_id" => $agent3_id,
                "agent4_id" => $agent4_id,
//                "profit_column" => $profit_column,
//                "fee_column" => $fee_column,
                "page" => $request->get("page", 1),
                "pagesize" => $request->get("pagesize", 20),
                "store_pid" => $storeP->store_id,
                "store_id" => $storeIds,
                "curHdIndex" => $request->get("curHdIndex"),
                "startDate" => $request->get("startDate"),
                "endDate" => $request->get("endDate"),
                "cashier_id" => $request->get("merchant_id"),
            ];
            $service = new MerchantBillCountService();
            list($list, $total) = $service->staticBillLogList($where);
            return $this->successResponse("ok", ["data" => $list, "total" => $total]);
        } catch (\Exception $e) {
            return $this->errorResponse($e->getMessage());
        }
    }


}
